package com.luisliuyi.lib.crash;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.luisliuyi.lib.crash.tool.ToolAppManager;
import com.luisliuyi.lib.crash.tool.ToolLogUtils;

/**
 * 生命周期管理
 */
public class LifecycleCallback implements Application.ActivityLifecycleCallbacks{
    public static LifecycleCallback getInstance() {
        return HolderClass.INSTANCE;
    }

    private final static class HolderClass {
        private final static LifecycleCallback INSTANCE = new LifecycleCallback();
    }

    private LifecycleCallback() {}

    /**
     * 必须在 Application 的 onCreate 方法中调用
     */
    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
        ToolLogUtils.e("Activity生命周期","onActivityCreated");
        ToolAppManager.getAppManager().addActivity(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        ToolLogUtils.e("Activity生命周期","onActivityStarted");
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        ToolLogUtils.e("Activity生命周期","onActivityResumed");
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        ToolLogUtils.e("Activity生命周期","onActivityPaused");
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        ToolLogUtils.e("Activity生命周期","onActivityStopped");
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
        ToolLogUtils.e("Activity生命周期","onActivitySaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        ToolLogUtils.e("Activity生命周期","onActivityDestroyed");
        //将当前Activity移除到容器
        ToolAppManager.getAppManager().removeActivity(activity);
    }
}
